// pages/userset/changephone/changephone.js
const api = require('../../../utils/interface.js')
const math = require("../../../utils/math.js")
const tools = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldphone:"",
    phone:"",
    code:"发送验证码",
    iscode: "",//用于存放验证码接口里获取到的code
    msgId:""//返回的id
  },
  getCode: function () {
    var a = this.data.phone;
    var _this = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      api.getCode({ phone: this.data.phone }).then(res => {
        console.log(res, "res")
        _this.setData({
          msgId: res.data.result
        })
        var num = 61;
        var timer = setInterval(function () {
          num--;
          if (num <= 0) {
            clearInterval(timer);
            _this.setData({
              code: '重新发送',
              disabled: false
            })

          } else {
            _this.setData({
              code: num + "s"
            })
          }
        }, 1000)
      })

    }
  },
  //获取验证码
  getVerificationCode() {
    console.log(this.data.phone)
    if (!(/^1[34578]\d{9}$/.test(this.data.phone))) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    
    this.getCode();
    var _this = this
    _this.setData({
      disabled: true
    })
  },

   // 手机号验证
   blurPhone: function(e) {
    var phone = e.detail.value;
      this.setData({
        phone:phone
      })
    
  },
  //验证码
  passWdInput(e){
    var code = e.detail.value;
    this.setData({
      iscode:code
    })
  },
   // 表单提交
  formSubmit(e) {
     console.log(e,"e")
    let that = this
     let memberId = wx.getStorageSync('userInfo').id
    let val = e.detail.value
     if (this.data.phone == "") {
       wx.showToast({
         title: '手机号不能为空',
         icon: 'none',
         duration: 1000
       })
       return false;
     }
       if (this.data.iscode == "") {
         wx.showToast({
           title: '验证码不能为空',
           icon: 'none',
           duration: 1000
         })
         return false;
       }
      api.updateMemberPhone({ newPhone: this.data.phone, msgId:this.data.msgId, code: this.data.iscode, memberId:memberId }).then(res => {
        if(res.data.status==200){
          wx.showModal({
            title: '提示',
            content: '修改成功',
            showCancel:false,
            success(){
              wx.clearStorageSync()
              wx.switchTab({
                url: '/pages/index/index'
              }); 
            }
          })
         
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.code,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          })
        }
      })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserinfo()
  },
  getUserinfo(e) {
    let user = wx.getStorageSync('userInfo')
    api.getMemberInfo({
      id: user.id
    }).then(res => {
      this.setData({
        oldphone: res.data.result.phone
      })
    })
  },
})